//
//  VCOptionModel.m
//  Version1.0_RectiveCocoa
//
//  Created by 吴可高 on 2018/1/11.
//  Copyright © 2018年 吴可高. All rights reserved.
//

#import "VCOptionModel.h"

static VCOptionModel * _vcOptionalModel = nil;
@interface VCOptionModel()
@property(nonatomic,strong) RACCommand * cmd;

@end
@implementation VCOptionModel
+(instancetype)shareInstance{
    static dispatch_once_t onceToken;
    if (!_vcOptionalModel) {
        dispatch_once(&onceToken, ^{
            _vcOptionalModel = [[VCOptionModel alloc]init];
            _vcOptionalModel.indefiner =@"123";
        });
    }
    return _vcOptionalModel;
}

-(void)rac_textsingalChanged:(NSObject*)obj{
    if ([obj isKindOfClass:[UITextField class]]) {
        UITextField * textField = (UITextField*)obj;
        /*
        [[textField.rac_textSignal filter:^BOOL(NSString * _Nullable value) {
            return value.length > 5;
        }]subscribeNext:^(NSString * _Nullable x) {
            NSLog(@"textField.text:%@\n",x);
        }];
        */
        /*
        [[[textField.rac_textSignal map:^id _Nullable(NSString * _Nullable value) {
            return @{@"key":value};
        }]filter:^BOOL(id  _Nullable value) {
            NSString * textValue = value[@"key"];
            return textValue.length > 4;
        }]subscribeNext:^(id  _Nullable x) {
            NSLog(@"textField.text:%@\n",x);
        }];
        */
        RACSignal *validUsernameSignal =
        [textField.rac_textSignal
         map:^id(NSString *text) {
             return @(text.length > 5 ? YES: NO);
         }];
        RAC(textField,backgroundColor) = [validUsernameSignal map:^id _Nullable(NSNumber *  value) {
            return [value boolValue] ? [UIColor grayColor] : [UIColor redColor];
        }];
    }
}
-(void)rac_conbineObj1:(UITextField*)obj1 obj2:(UITextField*)obj2 superView:(UIView*)superView{
    RACSignal *validUsernameSignal =
    [obj1.rac_textSignal
     map:^id(NSString *text) {
         return @(text.length > 5 ? YES: NO);
     }];
    RAC(obj1,backgroundColor) = [validUsernameSignal map:^id _Nullable(NSNumber *  value) {
        return [value boolValue] ? [UIColor grayColor] : [UIColor redColor];
    }];
    //
    RACSignal *validPasswordSignal =
    [obj2.rac_textSignal
     map:^id(NSString *text) {
         return @(text.length > 5 ? YES: NO);
     }];
    RAC(obj2,backgroundColor) = [validPasswordSignal map:^id _Nullable(NSNumber *  value) {
        return [value boolValue] ? [UIColor grayColor] : [UIColor redColor];
    }];
   RAC(superView,backgroundColor) = [[RACSignal combineLatest:@[validUsernameSignal,validPasswordSignal] reduce:^id _Nullable(NSNumber * userNumber,NSNumber * passNumber){
        return @([userNumber boolValue]&&[passNumber boolValue]);
   }]map:^id _Nullable(id  _Nullable value) {
       return [value boolValue] ? [UIColor yellowColor] : [UIColor blueColor];

   }];
}

-(void)loginConditionContainer:(UITextField*)container{
    /*
    //方法1
    [[RACObserve(container, text)filter:^BOOL(NSString*  _Nullable value) {
        return value.length > 2;
    }]subscribeNext:^(NSString* _Nullable x) {
        NSLog(@"scribe.text =%@",x);
    }];
    */
    /*
    //方法2：在返回结果前，拼接使用RAC中bind
    [[RACObserve(container, text)flattenMap:^__kindof RACSignal * _Nullable(id  _Nullable value) {
        return [RACReturnSignal return:[NSString stringWithFormat:@"wukg_xx_%@",value]];
    }]subscribeNext:^(id  _Nullable x) {
        NSLog(@"%@",x);
    }];
    */
    /*
    //方法3：在返回结果前，拼接使用RAC中bind
    [[RACObserve(container, text)bind:^RACSignalBindBlock _Nonnull{
        return  ^RACSignal*(id value,BOOL *stop){
            return [RACReturnSignal return:[NSString stringWithFormat:@"_____%@",value]];
        };
    }]subscribeNext:^(id  _Nullable x) {
        NSLog(@"%@",x);
    }];
    */
    /*
    //方法4：在返回结果前，拼接使用RAC中flattenMap
    [[RACObserve(container, text)flattenMap:^__kindof RACSignal * _Nullable(id  _Nullable value) {
        return [RACReturnSignal return:[NSArray arrayWithObject:value]];
    }]subscribeNext:^(id  _Nullable x) {
        NSLog(@"%@",x);
    }];
    */
    /*
    [[RACObserve(container, text)map:^id _Nullable(id  _Nullable value) {
        return [NSString stringWithFormat:@"idefiner:%@",value];
    }]subscribeNext:^(id  _Nullable x) {
        NSLog(@"%@",x);
    }];
    */
}

//ReactiveCocoa操作方法之组合
-(void)senderConcatSessionWithContainer:(UITextField*)container1 container:(UITextField*)container2{
    /*
    RACSignal *signalA = [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
        [subscriber sendNext:@"signalA"];
        [subscriber sendCompleted];
        return nil;
    }];
    
    RACSignal *signalB = [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
        [subscriber sendNext:@"signalB"];
        [subscriber sendCompleted];
        return nil;
    }];
    //concat:按照一定顺序拼接信号，当多个信号发出之后，有顺序接受信号
    [[signalA concat:signalB]subscribeNext:^(id  _Nullable x) {
        NSLog(@"subcribe_text:%@",x);
    }];
    */
    /*
    //then:用于连接二个信号，当第一个信号完成，才会连接then返回的信号
    [[[RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
        [subscriber sendNext:@"subcribe:A"];
        [subscriber sendCompleted];
        return nil;
    }]then:^RACSignal * _Nonnull{
        return [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
            [subscriber sendNext:@"subcribe:B"];
            [subscriber sendCompleted];
            return nil;
        }];
    }]subscribeNext:^(id  _Nullable x) {
        NSLog(@"%@",x);
    }];
     */
    /*
    //merge:把多个信号合并成一个信号，任何一个信号有新值时候都会调用
    RACSignal * text1Signal =RACObserve(container1, text);
    RACSignal * text2Signal =RACObserve(container2, text);
    [[text1Signal merge:text2Signal]subscribeNext:^(id  _Nullable x) {
        NSLog(@"%@",x);
    }];
    */
    /*
    //zipWith:把两个信号压缩成一个信号，只有当两个信号同时发出信号内容时，并且把两个信号的内容合并成一个元组，才会触发压缩流的next事件
    RACSignal * text1Signal =RACObserve(container1, text);
    RACSignal * text2Signal =RACObserve(container2, text);
    [[text1Signal zipWith:text2Signal]subscribeNext:^(id  _Nullable x) {
        NSLog(@"%@",x);
    }];
    */
    /*
    //combineLatest:将多个信号合并起来，并且拿到各个信号的最新的值,必须每个合并的signal至少都有过一次sendNext，才会触发合并的信号
    RACSignal * text1Signal =RACObserve(container1, text);
    RACSignal * text2Signal =RACObserve(container2, text);
    [[text1Signal combineLatestWith:text2Signal]subscribeNext:^(id  _Nullable x) {
        NSLog(@"%@",x);
    }];
    */
    /*
    //reduce`聚合:用于信号发出的内容是元组，把信号发出元组的值聚合成一个值
    RACSignal * text1Signal =RACObserve(container1, text);
    RACSignal * text2Signal =RACObserve(container2, text);
    [[[text1Signal combineLatestWith:text2Signal]reduceEach:^id _Nullable(NSString * formate1,NSString*formate2){
        return [NSString stringWithFormat:@"%@%@",formate1,formate2];
    }]subscribeNext:^(id  _Nullable x) {
        NSLog(@"%@",x);
    }];
    */
    
}

-(void)senderFilterWithContriner:(UITextField*)container{
    //filter:过滤信号
    [[RACObserve(container, text)filter:^BOOL(id  _Nullable value) {
        if ([value isKindOfClass:[NSString class]]) {
            NSString * vaildtext = (NSString*)value;
            return  vaildtext.length > 3;
        }
        return nil;
    }]subscribeNext:^(id  _Nullable x) {
        NSLog(@"vaildtext:%@",x);
    }];
    //ignore:忽略某些特定的值
    [[RACObserve(container, text)ignore:@"1"]subscribeNext:^(id  _Nullable x) {
        NSLog(@"unignore:%@",x);
    }];
    //当上一次的值和这次值有明显变化时才会发出信号
    [[container.rac_textSignal distinctUntilChanged]subscribeNext:^(NSString * _Nullable x) {
        NSLog(@"distinctValueChanged:%@",x);
    }];
    //takeUntil：获取信号直到某个信号执行完成
    //[container.rac_textSignal takeUntil:container.rac_willDeallocSignal];
    //skip:(NSUinteger)跳过几个信号，不接受
    [[container.rac_textSignal skip:2]subscribeNext:^(NSString * _Nullable x) {
        NSLog(@"skip:(value):%@",x);
    }];
    
}

//ReactiveCocoa操作方法之次序
-(void)senderSequenceWithContainer:(UITextField*)container{
    /*
    [[[[RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
        //在执行[subscriber sendNext:@"1"]之前会先执行“NSLog(@"doNext")”
        [subscriber sendNext:@"1"];
        //在执行[subscriber sendCompleted]之前会先执行“NSLog(@"doCompleted")”
        [subscriber sendCompleted];
        return nil;
    }]doNext:^(id  _Nullable x) {
        NSLog(@"doNext");
    }]doCompleted:^{
        NSLog(@"doCompleted");
    }]subscribeNext:^(id  _Nullable x) {
        NSLog(@"%@===",x);
    }];
    */
    [self arc_thread_timer_option:container];
}

-(void)arc_thread_timer_option:(UITextField*)container{
    /*
    //deliverOn: 内容传递切换到制定线程中
    //时间：timeout：超时，可以让一个信号在一定的时间后，自动报错
    RACSignal * signal = [RACObserve(container, text) timeout:2.f onScheduler:[RACScheduler currentScheduler]];
    [signal subscribeNext:^(id  _Nullable x) {
        NSLog(@"%@__x",x);
    } error:^(NSError * _Nullable error) {
        NSLog(@"error:%@",error.description);
    }];
    //interval:定时 每隔一段时间发出信号
    [[RACSignal interval:2.f onScheduler:[RACScheduler currentScheduler]]subscribeNext:^(NSDate * _Nullable x) {
    }];
    //delay:延迟发送next
    [[RACObserve(container, text)delay:3.f]subscribeNext:^(id  _Nullable x) {
        NSLog(@"delay_conductValue:%@",x);
    }];
    
    //操作方法：重复只要失败，就会重新执行创建信号中的block,直到成功.
    __block int i = 0;
    [[[RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
        if (10==i) {
            [subscriber sendNext:@"1"];
        }else{
            NSLog(@"接收到错误");
            [subscriber sendError:nil];
        }
        return nil;
    }]retry]subscribeNext:^(id  _Nullable x) {
        NSLog(@"subcribeNext:%@",x);
    } error:^(NSError * _Nullable error) {
        NSLog(@"error:%@",error.description);
    }];
    */
    //replay:当一个信号被多次订阅，反复播放内容
    RACSignal * signalReplay = [[RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
        [subscriber sendNext:@"1"];
        [subscriber sendNext:@"2"];
        [subscriber sendNext:@"3"];

        return nil;
    }]replay];
    [signalReplay subscribeNext:^(id  _Nullable x) {
        NSLog(@"第一个订阅者%@",x);
    }];
    [signalReplay subscribeNext:^(id  _Nullable x) {
        NSLog(@"第二个订阅者%@",x);
    }];
   self.cmd =[[RACCommand alloc]initWithSignalBlock:^RACSignal * _Nonnull(id  _Nullable input) {
        RACSignal * commend_siganl = [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
            [subscriber sendNext:@""];
            [subscriber sendCompleted];
            return nil;
        }];
        return [commend_siganl map:^id _Nullable(id  _Nullable value) {
            return value;
        }];
    }];
}




@end

